<template>
  <el-dialog
    :title="!dataForm.yhId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户账号" prop="yhName">
      <el-input v-model="dataForm.yhName" placeholder="用户账号"></el-input>
    </el-form-item>
    <el-form-item label="用户密码" prop="yhPwd">
      <el-input v-model="dataForm.yhPwd" placeholder="用户密码"></el-input>
    </el-form-item>
    <el-form-item label="用户姓名" prop="yhNickname">
      <el-input v-model="dataForm.yhNickname" placeholder="用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="用户性别" prop="yhSex">
      <el-input v-model="dataForm.yhSex" placeholder="用户性别"></el-input>
    </el-form-item>
    <el-form-item label="用户电话" prop="yhTel">
      <el-input v-model="dataForm.yhTel" placeholder="用户电话"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          yhId: 0,
          yhName: '',
          yhPwd: '',
          yhNickname: '',
          yhSex: '',
          yhTel: ''
        },
        dataRule: {
          yhName: [
            { required: true, message: '用户账号不能为空', trigger: 'blur' }
          ],
          yhPwd: [
            { required: true, message: '用户密码不能为空', trigger: 'blur' }
          ],
          yhNickname: [
            { required: true, message: '用户姓名不能为空', trigger: 'blur' }
          ],
          yhSex: [
            { required: true, message: '用户性别不能为空', trigger: 'blur' }
          ],
          yhTel: [
            { required: true, message: '用户电话不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.yhId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.yhId) {
            this.$http({
              url: this.$http.adornUrl(`//user/info/${this.dataForm.yhId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.yhName = data.user.yhName
                this.dataForm.yhPwd = data.user.yhPwd
                this.dataForm.yhNickname = data.user.yhNickname
                this.dataForm.yhSex = data.user.yhSex
                this.dataForm.yhTel = data.user.yhTel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`//user/${!this.dataForm.yhId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'yhId': this.dataForm.yhId || undefined,
                'yhName': this.dataForm.yhName,
                'yhPwd': this.dataForm.yhPwd,
                'yhNickname': this.dataForm.yhNickname,
                'yhSex': this.dataForm.yhSex,
                'yhTel': this.dataForm.yhTel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
